import storage from 'store'
// import { login, getInfo, logout } from '@/api/auth/login'
import { loginMobileVerifyCode, getCurrentUser } from '@/api/flow/user'
import { postLogin, getInfo, logout } from '@/api/auth'
import { ACCESS_TOKEN, ACCESS_RISK_TOKEN } from '@/store/mutation-types'
import roles from '@/utils/roles'
import { getAuths } from '@/api/flow/auth'

const user = {
  state: {
    token: '',
    r_token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    authentication: false,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, { name, welcome }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
  },

  actions: {
    // 登录验证
    LoginMobileVerifyCode ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        loginMobileVerifyCode(userInfo).then(response => {
          const { token } = response

          // flowengine token
          storage.set(ACCESS_TOKEN, token)
          commit('SET_TOKEN', token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登录
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        postLogin(userInfo).then(response => {
          console.log('res:', response)
          const { token } = response
          const t = `gongdan ${token}`
          storage.set(ACCESS_TOKEN, t)
          commit('SET_TOKEN', t)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo ({ commit }) {
      return new Promise((resolve, reject) => {
        getCurrentUser().then(response => {
          // if (response.role && response.role.permissions.length > 0) {
          //   // const role = response.role
          //   // role.permissions = response.role.permissions
          //   // role.permissions.map(per => {
          //   //   if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
          //   //     const action = per.actionEntitySet.map(action => { return action.action })
          //   //     per.actionList = action
          //   //   }
          //   // })
          //   // role.permissionList = role.permissions.map(permission => { return permission.permissionId })
          //   // commit('SET_ROLES', 'admin')
          //   commit('SET_INFO', response)
          // } else {
          //   // reject(new Error('getInfo: roles must be a non-null array !'))
          // }

          getAuths()
            .then((data) => {
              const role = roles
              // role.permissions = roles.permissions
              // role.permissions.map(per => {
              //   if (per.actionEntitySet != null && per.actionEntitySet.length > 0) {
              //     const action = per.actionEntitySet.map(action => { return action.action })
              //     per.actionList = action
              //   }
              // })
              // role.permissionList = data.alias

              commit('SET_ROLES', data.alias)

              commit('SET_INFO', response)

              commit('SET_NAME', { name: response.username, welcome: '' })
              commit('SET_AVATAR', response.avatar)

              resolve({ ...response, role: data.alias })
            })
            .catch((error) => {
              reject(error)
            })
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        }).finally(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          storage.remove(ACCESS_TOKEN)
        })
      })
    },

  },
}

export default user
